<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>http or https site file validation</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js'></script>
    <script src="https://cdn.bootcss.com/babel-polyfill/7.4.4/polyfill.min.js"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js'></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
        integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.min.js"></script>
        
</head>

<body>
    <div id="app" class="container-fluid">

        <div>
            <h1>ALL PATHS:</h1>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>delete</th>
                        <th>Path</th>
                        <th>Value</th>
                        <th>Create</th>
                        <th>Last Access</th>
                        <th>access</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in requests" :key="index">
                        <td>
                            <a href="javascript:void(0)" @click="remove(item.id)" title="点击删除">delete</a>
                        </td>
                        <td>{{item['path']}}</td>
                        <td>{{item['value']}}</td>
                        <td>{{item['date'] | formatDate}}</td>
                        <td>{{item['lastAccess'] | formatDate}}</td>
                        <td>
                            <a :href="item['path']" target="_blank">access</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <hr />
        <form @submit.prevent="return false">
            <div class="form-group">
                <label>path:</label>
                <input name="path" id="path" v-model="path" size="100" class="form-control" />
            </div>
            <div class="form-group">
                <label>Content-Type:</label>
                <input id='contentType' v-model='contentType' class="form-control"/>
            </div>
            <div class="form-group">
                <label>value:</label>
                <textarea id='value' v-model='value' cols=70 rows=10 class="form-control"></textarea>
            </div>
            <div class="form-group">
                <button @click='newurl' class="btn btn-default">Create/Update</button>
            </div>
        </form>
        <script>
            var vm = new Vue({
                el: "#app",
                created() {
                    this.fetch();
                },
                data: {
                    requests: [],
                    path: "/.well-known/pki-validation/wotrus.txt",
                    value: "test",
                    contentType:"text/plain;charset=utf-8"
                },
                filters: {
                    formatDate(time) {
                        if(time)
                            return moment(time).fromNow();
                        else
                            return "";
                    }
                },
                methods: {
                    fetch:function(){
                        axios.get('/data?r='+Math.random(), {

                        }).then(function (response) {
                                vm.requests = response.data;
                        })
                        .catch(function (error) {
                        })
                    },
                    newurl: function () {
                        var self = this;
                        axios.post("/newurl", { path: this.path, value: this.value ,contentType:this.contentType })
                        .then(function (response) {
                            //console.log(response);
                            $("#tipdialog").modal('show');
                            self.fetch();
                            setTimeout(function(){
                                $("#tipdialog").modal('hide');
                            },2500)
                        })
                    },
                    remove: function (id) {
                        axios.delete("/delete", {
                            params: {
                                id: id
                            }
                        })
                        .then(function (response) {
                            vm.requests = vm.requests.filter(
                                function (item) {
                                    return item.id != id
                                }
                            );
                        })
                        .catch(function (error) {
                        })
                    }
                }
            })
        </script>
    </div>

    <div id="tipdialog" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title">tip</h4>
            </div>
            <div class="modal-body">
              <p class="text text-success">successful</p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</body>

</html>